<?php

namespace App\Models\Order;

use App\Models\BasicModel;
use App\Models\User;
use App\Services\Alipay;
use App\Services\Data;
use App\Services\Wechat;
use Dcat\Admin\Traits\HasDateTimeFormatter;


class RechargeOrder extends BasicModel
{
	use HasDateTimeFormatter;


    protected $table = 'recharge_order';


    public function order($uid,$opt_id,$type){

        $opt=RechargeOption::find($opt_id);
        $data=[
            'user_id'=>$uid,
            'order_no'=>'C'.str_pad($uid,5,'0',STR_PAD_LEFT).Data::orderTimeNumber(),
            'price' =>$opt->price,
            'money' =>$opt->amount,
            'pay_type' =>$type,
        ];
        return self::create($data);

    }

    public function pay($oid,$pay_type){
        $order=self::find($oid);
        if($pay_type==1){
            $result = (new Alipay())->app($order->order_no,$order->price);
        }elseif ($pay_type==2){
            $result = (new Wechat())->app($order->order_no,$order->price);
        }
        return $result;
    }

    

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

}
